#!/usr/bin/python
'''
/*
 * This computer program is the confidential information and proprietary trade
 * secret of VistaraIT LLC.  Possessions and use of this program must  conform
 * strictly to the license agreement between the user and VistaraIT LLC.,  and
 * receipt or possession does not convey any rights to divulge, reproduce,  or
 * allow others to use this program without specific written authorization  of
 * VistaraIT LLC.
 * 
 * Copyright (c) 2014 VistaraIT, Inc. All rights reserved.
 */
'''

from xml.dom.minidom import parseString

class XMLReader():
    
    def __init__(self, xml):
        self.xml = xml
        self.xmlData = {}

    def processData(self, startNode):
        dom = parseString(self.xml)
        root = dom.getElementsByTagName(startNode)[0];
        self.processNode(root, "")
        return self.xmlData

    def processNode(self, node, rootPath):
        nodeName = node.nodeName
        curPath = rootPath
        if curPath != "":
            curPath = curPath + "."
        curPath = curPath + nodeName
        if nodeName != 'services' and nodeName != 'packages':
            #Read attributes
            attributes = node.attributes
            if attributes and attributes.length > 0:
                for i in range(attributes.length):
                    attr = attributes.item(i)
                    self.xmlData[curPath + "." + attr.name] = str(attr.value)
            if node.nodeType == node.TEXT_NODE:
                self.xmlData[rootPath] = str(node.data)
            else:
                #read child nodes
                childNodes = node.childNodes
                for child in childNodes:
                    self.processNode(child, curPath) 
        else:
            if nodeName == 'services':
                childNodeName = 'service'
            elif nodeName == 'packages':
                childNodeName = 'package'
            serviceNodes = node.getElementsByTagName(childNodeName)
            services = [];
            for serviceNode in serviceNodes:
                service = self.getServiceDetails(serviceNode)
                services += [service]
            self.xmlData[curPath] = services

    def getServiceDetails(self,serviceNode):
        service = {}
        #read attributes
        attributes = serviceNode.attributes
        if attributes and attributes.length > 0:
            for i in range(attributes.length):
                attr = attributes.item(i)
                service[attr.name] = str(attr.value)

        #Get child nodes
        childNodes = serviceNode.childNodes
        for childNode in childNodes:
            if childNode.nodeName != "tars":
                service[childNode.nodeName] = self.getText(childNode)
            else:
                nodes = childNode.childNodes
                tagNodes = [];
                for child in nodes:
                    tagNode = self.getServiceDetails(child)
                    tagNodes += [tagNode]
                service[childNode.nodeName] = tagNodes
        return service

    def getText(self,node):
        childNodes = node.childNodes
        for childNode in childNodes:
            if childNode.nodeType == childNode.TEXT_NODE:
                return childNode.data
        return ""
